﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Project13776616780_01.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Project13776616780_01
{
    [Route("api")]
    public class HomeController : ControllerBase
    {
        private readonly GlobalConfig config;

        public HomeController(GlobalConfig config)
        {
            this.config = config;
        }
        [HttpPost]
        [Route("upload")]
        public async Task<IActionResult> SaveImage([FromForm] IFormFile fileContent)
        {
            var path = Path.Combine(config.TargetPath, "images");
            var stream = fileContent.OpenReadStream();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var fileName = Guid.NewGuid().ToString() + "." + fileContent.FileName.Split('.').LastOrDefault();
            var filePath = Path.Combine(path, fileName);
            var ms = new MemoryStream();
            stream.CopyTo(ms);
            ms.Seek(0, SeekOrigin.Begin);
            await System.IO.File.WriteAllBytesAsync(filePath, ms.ToArray());
            ms.Dispose();
            return Ok(new
            {
                code = 0,
                url = $"{config.ResourceHost}/images/" + fileName
            });
        }
        [HttpPost]
        [Route("upload/logo")]
        public async Task<IActionResult> SaveLogoImage([FromForm] IFormFile fileContent)
        {
            var path = Path.Combine(config.TargetPath, "images");
            var stream = fileContent.OpenReadStream();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var fileName = "logo." + fileContent.FileName.Split('.').LastOrDefault();
            var filePath = Path.Combine(path, fileName);
            var ms = new MemoryStream();
            stream.CopyTo(ms);
            ms.Seek(0, SeekOrigin.Begin);
            await System.IO.File.WriteAllBytesAsync(filePath, ms.ToArray());
            ms.Dispose();
            return Ok(new
            {
                code = 0,
                url = $"{config.ResourceHost}/images/" + fileName
            });
        }
        [HttpPost]
        [Route("upload/adv")]
        public async Task<IActionResult> SaveAdvImage([FromForm] IFormFile fileContent)
        {
            var path = Path.Combine(config.TargetPath, "images");
            var stream = fileContent.OpenReadStream();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var fileName = "ad." + fileContent.FileName.Split('.').LastOrDefault();
            var filePath = Path.Combine(path, fileName);
            var ms = new MemoryStream();
            stream.CopyTo(ms);
            ms.Seek(0, SeekOrigin.Begin);
            await System.IO.File.WriteAllBytesAsync(filePath, ms.ToArray());
            ms.Dispose();
            return Ok(new
            {
                code = 0,
                url = $"{config.ResourceHost}/images/" + fileName
            });
        }
    }
}
